package crawler.web.beans;

import java.io.Serializable;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import library.utils.IP;

import org.apache.commons.lang.StringUtils;

/**
 * @author waynechen Feb 7, 2012 8:32:40 AM
 */
public class CrawlURL implements Serializable {
	private static final long serialVersionUID = 6426110091367858655L;
	private static final Pattern domainPattern = Pattern.compile("http://([^/]*?)/.*");

	public CrawlURL() {
	}

	public CrawlURL(String oriUrl) {
		this.oriUrl = oriUrl;
	}

	private String ip;//主机ip

	public String getIp() throws UnknownHostException {
		if (StringUtils.isEmpty(ip)) {
			Matcher matcher = domainPattern.matcher(getOriUrl());
			while (matcher.find()) {
				ip = IP.hostName2IP(matcher.group(1));
				url = getOriUrl().replace(matcher.group(1), ip);
				break;
			}
		}
		return ip;
	}

	public String getUrl() throws UnknownHostException {
		if (StringUtils.isEmpty(url)) {
			getIp();
		}
		return url;
	}

	private Long lastCrawlTime = new Long(new Date().getTime());//上次爬的时间

	public Long getLastCrawlTime() {
		return lastCrawlTime;
	}

	public void setLastCrawlTime() {
		this.lastCrawlTime = new Long(new Date().getTime());
	}

	private String oriUrl;//原始URL的值,主机部分是域名

	@Override
	public String toString() {
		return "CrawlURL [ip=" + ip + ", lastCrawlTime=" + lastCrawlTime + ", oriUrl=" + oriUrl + ", html=" + html + ", proxy=" + proxy + ", url=" + url + ", urlNo=" + urlNo + ", statusCode=" + statusCode + ", hitNum=" + hitNum + ", encoding=" + encoding + ", abstractText=" + abstractText + ", author=" + author + ", weight=" + weight + ", description=" + description + ", fileSize=" + fileSize + ", lastUpdateTime=" + lastUpdateTime + ", timeToLive=" + timeToLive + ", title=" + title + ", type=" + type + ", urlReference=" + Arrays.toString(urlReference) + ", layer=" + layer + ", retryTimes=" + retryTimes + "]";
	}

	private String html;//本url对应的html,由爬虫下载后,此字段才有值
	private String proxy;// 本url所指的主机用的代理(只有进hosts库的才有值)
	private String url;// url的值, 主机部分是ip , 为了防止 重复主机的出现
	private Integer urlNo;//url number
	private Integer statusCode;//获取url返回的结果码
	private Integer hitNum;//此url被其它文章引用的次数
	private String encoding;//此url对应的文章的汉字编码
	private String abstractText;//文章的摘要
	private String author;//作者
	private Integer weight;//文章的权重(包含导向词的信息)
	private String description;//文章的描述
	private Integer fileSize;//文章的大小
	private Long lastUpdateTime;//最后修改时间 
	private Date timeToLive;//过期时间
	private String title;//文章名称
	private String type;// 文章类型
	private String[] urlReference;//引用的链接, page rank时用
	private Integer layer = 0;//爬取的层次,从种子开始,依次为第0层, 第1层,...
	private Integer retryTimes = 0;//抓取失败次数

	public Integer getRetryTimes() {
		return retryTimes;
	}

	public void setRetryTimes(Integer retryTimes) {
		this.retryTimes = retryTimes;
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public String getProxy() {
		return proxy;
	}

	public void setProxy(String proxy) {
		this.proxy = proxy;
	}

	public Long getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(Long lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	public String getOriUrl() {
		return oriUrl;
	}

	public void setOriUrl(String oriUrl) {
		this.oriUrl = oriUrl;
	}

	public Integer getUrlNo() {
		return urlNo;
	}

	public void setUrlNo(Integer urlNo) {
		this.urlNo = urlNo;
	}

	public Integer getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(Integer statusCode) {
		this.statusCode = statusCode;
	}

	public Integer getHitNum() {
		return hitNum;
	}

	public void setHitNum(Integer hitNum) {
		this.hitNum = hitNum;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public String getAbstractText() {
		return abstractText;
	}

	public void setAbstractText(String abstractText) {
		this.abstractText = abstractText;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Integer getWeight() {
		return weight;
	}

	public void setWeight(Integer weight) {
		this.weight = weight;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getFileSize() {
		return fileSize;
	}

	public void setFileSize(Integer fileSize) {
		this.fileSize = fileSize;
	}

	public Date getTimeToLive() {
		return timeToLive;
	}

	public void setTimeToLive(Date timeToLive) {
		this.timeToLive = timeToLive;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String[] getUrlReference() {
		return urlReference;
	}

	public void setUrlReference(String[] urlReference) {
		this.urlReference = urlReference;
	}

	public Integer getLayer() {
		return layer;
	}

	public void setLayer(Integer layer) {
		this.layer = layer;
	}

}

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//